<?php
/**
 * Created by PhpStorm.
 * User: dong
 * Date: 2018/4/17
 * Time: 20:41
 */
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\Contracts\Actions\HrefRedirectAction;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;
use function GuzzleHttp\Psr7\uri_for;
use Illuminate\Support\Facades\Config;

class AlipayPayment extends RechargeBaseAbstract implements WAPable
{

    protected $random_body = [
        '小商品',
        '零食',
        '化妆品',
        '服装订购',
        '水果采购',
        '办公用品',
        '生活用品',
        '家装建材'
    ];

    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($this->veryCallbackSign($data)) {
            if ($data['trade_status'] == 'TRADE_SUCCESS' || $data['trade_status'] == 'TRADE_FINISHED') {
                return true;
            }
        }
        return false;
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $webService = app('alipay.web');
        $wapService = app('alipay.wap');
        $webService->setPartner($this->getMchId());
        $webService->setKey($this->getMchKey());
        $wapService->setPartner($this->getMchId());
        $wapService->setKey($this->getMchKey());
        if ($webService->verify() || $wapService->verify()) {
            return true;
        }
        return false;
    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
        return $data['out_trade_no'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return $data['total_fee'];
    }

    public function getThirdNoFromCallback(array $data)
    {
        return $data['trade_no'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::ALIPAY;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
        // 创建支付单。
        $alipay = app('alipay.web');
        $alipay->setPartner($this->getMchId());
        $alipay->setKey($this->getMchKey());
        $alipay->setNotifyUrl($this->getCallbackUrl());
        $alipay->setReturnUrl($this->getReturnUrl());
        $alipay->setOutTradeNo($rechargeOrder->plat_no);
        $alipay->setTotalFee($rechargeOrder->order_amt);
        $alipay->setSubject(array_key_exists('body', $rechargeOrder->order_data) ? $rechargeOrder->order_data['body'] : $this->getRandomBody());
        $alipay->setBody($this->setRemark($rechargeOrder));

        //$alipay->setQrPayMode('4'); //该设置为可选，添加该参数设置，支持二维码支付。

        // 跳转到支付页面。
        return $alipay->getPayLink();

    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return 'success';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement initParameters() method.
    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        // 创建支付单。
        $alipay = app('alipay.wap');
        $alipay->setPartner($this->getMchId());
        $alipay->setKey($this->getMchKey());
        $alipay->setNotifyUrl($this->getCallbackUrl());
        $alipay->setReturnUrl($this->getReturnUrl());
        $alipay->setOutTradeNo($rechargeOrder->plat_no);
        $alipay->setTotalFee($rechargeOrder->order_amt);
        $alipay->setSubject(array_key_exists('body', $rechargeOrder->order_data) ? $rechargeOrder->order_data['body'] : $this->getRandomBody());
        $alipay->setBody($this->setRemark($rechargeOrder));

        // 返回签名后的支付参数给支付宝移动端的SDK。
        return new HrefRedirectAction($alipay->getPayLink());
    }

    public function getRandomBody()
    {
        return array_random($this->random_body);
    }
}
